import requests
import json

if __name__ == '__main__':
    # 1 获取url
    url = "https://fanyi.baidu.com/sug"
    # 2 伪装UA
    pretend_header = {
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36"
    }
    # 3 配置data
    words = input("输入查询单词:")
    myDatas = {
        "kw": words
    }
    # 4 发起请求
    response = requests.post(url, data=myDatas, headers=pretend_header)
    # 5 获取返回数据，Content-Type: application/json
    response_json = response.json()

    # 分析
    for item in response_json['data']:
        print("查询单词:", item['k'])
        print("释义:", item['v'])